package gui;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import gui.graphs3d.Graph3D;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;

import main.ToEngineFacade;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class OutputTabs extends javax.swing.JFrame {

    private JTabbedPane jTabbedPane1;
    private TxtOutput txtOutput;
    private JToggleButton saveButton;
    private JButton doneButton;
    private JPanel jPanel1;
    private Graph3D graphOutput;
    private ToEngineFacade _toEngineFacade;

    public OutputTabs(TxtOutput txtOutput, Graph3D graphOutput) {
        super();
        this.txtOutput = txtOutput;
        this.graphOutput = graphOutput;
        initGUI();
    }

    private void initGUI() {
        try {
            GridLayout thisLayout = new GridLayout(1, 1);
            thisLayout.setHgap(5);
            thisLayout.setVgap(5);
            thisLayout.setColumns(1);
            this.setLayout(thisLayout);
            setPreferredSize(new Dimension(400, 300));
            {
                jPanel1 = new JPanel();
                AnchorLayout jPanel1Layout = new AnchorLayout();
                jPanel1.setLayout(jPanel1Layout);
                getContentPane().add(jPanel1);
                jPanel1.setPreferredSize(new java.awt.Dimension(392, 266));
                jPanel1.setLayout(jPanel1Layout);
                {
                    jTabbedPane1 = new JTabbedPane();
                    jPanel1.add(jTabbedPane1, new AnchorConstraint(5, 0, 24, 0, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
                    jTabbedPane1.setPreferredSize(new java.awt.Dimension(392, 237));
                    {
                        jTabbedPane1.addTab("Text output", null, txtOutput, null);
                    }
                    {
                        if (graphOutput != null) {
                            jTabbedPane1.addTab("Graph output", null, graphOutput, null);
                        }
                    }
                }
                {
                    doneButton = new JButton();
                    jPanel1.add(doneButton, new AnchorConstraint(911, 177, 0, 0, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
                    doneButton.setText("Done");
                    doneButton.setPreferredSize(new java.awt.Dimension(69, 24));
                    doneButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {

                            if (jTabbedPane1.getTabCount() > 1) {
                                jTabbedPane1.remove(1);
                            }
                            setVisible(false);
                        }
                    });
                }
                {
                    saveButton = new JToggleButton();
                    jPanel1.add(saveButton, new AnchorConstraint(915, 0, 0, 820, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_NONE));
                    saveButton.setText("Save");
                    saveButton.setPreferredSize(new java.awt.Dimension(71, 23));
                    saveButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            _toEngineFacade.save();
                            JOptionPane.showMessageDialog(null,  "Your graph has been saved", "Save operation", JOptionPane.INFORMATION_MESSAGE);
                        }
                    });
                }
            }
            pack();
            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addGraphOutput(Graph3D g) {
        if (jTabbedPane1.getTabCount() > 1) {
            jTabbedPane1.remove(1);
        }
        graphOutput = g;
        jTabbedPane1.insertTab("Graph output", null, graphOutput, "graph output", jTabbedPane1.getTabCount());
        jTabbedPane1.setSelectedIndex(1);
        graphOutput.requestFocus();
    }
    
    public void removeGraphInput(){
    	if (jTabbedPane1.getTabCount() > 1) {
            jTabbedPane1.remove(1);
        }
    }

    public void displayTxt(String txt) {
        txtOutput.displayTxt(txt);
    }

    public void setToEngineFacade(ToEngineFacade toEngineFacade) {
        _toEngineFacade = toEngineFacade;
    }
}
